<template>
	<view>
		<uv-notify ref="notify" customStyle="z-index:9999"></uv-notify>
		<div class="add-bike">
			<h1 class="title">修改自行车</h1>
			<form @submit.prevent="putBike" class="form">
				<label for="batteryId" class="label">电池ID:</label>

				<uv-input placeholder="请输入数字" border="surround" v-model="batteryId" class="input" clearable></uv-input>
				<label for="latitude" class="label">纬度:</label>

				<uv-input placeholder="请输入数字" border="surround" v-model="latitude" class="input" clearable></uv-input>
				<label for="longitude" class="label">经度:</label>

				<uv-input placeholder="请输入数字" border="surround" v-model="longitude" class="input" clearable></uv-input>
				<label for="lightThreshold" class="label">光照阈值:</label>

				<uv-input placeholder="请输入数字" border="surround" v-model="lightThreshold" class="input"
					clearable></uv-input>
				
					<uv-tags text="同步时间到设备" plain size="large" icon="tags" color="#00aa00"
						borderColor="#00aa00" class="label"></uv-tags>
					<uv-switch v-model="isSyncTime" class="label"></uv-switch>
				
				<button type="submit" class="button" @click="putBike">修改自行车</button>
			</form>
		</div>
	</view>

</template>

<script>
	import {
		func
	} from '../../../uni_modules/uv-ui-tools/libs/function/test';
	export default {
		data() {
			return {
				apiUrl: '',
				setToken: func(),
				header: {},
				bike: {},
				bikeId: '',
				batteryId: '',
				latitude: 0.0,
				longitude: 0.0,
				lightThreshold: 0.0,
				isSyncTime: false
			};
		},

		onLoad(options) {

			this.bike = JSON.parse(options.bike);
			console.log(options.bike);
			this.bikeId = this.bike.bikeId;
			this.batteryId = this.bike.batteryId;
			this.latitude = this.bike.latitude;
			this.longitude = this.bike.longitude;
			this.lightThreshold = this.bike.lightThreshold;
			// console.log('bike', bike);
		},
		mounted() {
			this.apiUrl = getApp().globalData.apiUrl;
			// setToken是设置header放入token的函数
			this.setToken = getApp().globalData.setToken;
			this.header = this.setToken();
			/* 如果没有用户信息或者无token直接返回登录重新获取 */
			if (this.user == '' || this.setToken == null) {
				this.promptFail("请先登录！");
				setTimeout(function() {
					uni.navigateTo({
						url: '/pages/user/login'
					});
				}, 1000);
			}
		},
		methods: {
			/* 成功提示 */
			promptSuccess(msg) {
				this.$refs.notify.success(msg);
			},
			promptFail(msg) {
				this.$refs.notify.error(msg);
			},
			putBike() {
				// 构造要发送的数据

				this.bike.batteryId = this.batteryId;
				this.bike.latitude = this.latitude;
				this.bike.longitude = this.longitude;
				this.bike.lightThreshold = this.lightThreshold;

				/* 查询所有可租借的车（无论是否借车） */
				const backendURL = this.apiUrl + "/bicycle/putOne";
				let header = this.setToken();
				let satoken = header.satoken;
				uni.request({
					url: backendURL,
					method: 'PUT',
					dataType: 'json',
					data: JSON.stringify(this.bike), // 将user对象转换为JSON字符串
					header: {
						'content-type': 'application/json', // 设置content-type为application/json
						'satoken': satoken
					},
					success: (res) => {
						console.log('成功', res);
						console.log(res.data);
						if (res.data.code == 200) {
							this.sendSet();
							this.promptSuccess(res.data.msg);
						} else {
							this.promptFail("请检查输入内容是否合法");
						}
					},
					fail: function(res) {
						console.log('错误', res);
						thi.promptFail("添加失败");
					}
				});
			},
			sendSet() {
				const backendURL = this.apiUrl + "/publish/setting?" + "bikeId=" + this.bikeId +
					"&lightThreshold=" + this.lightThreshold + "&isSyncTime=" + this.isSyncTime;
				console.log(backendURL);
				let header = this.setToken();
				
				let satoken = header.satoken;
				uni.request({
					url: backendURL,
					method: 'POST',
					dataType: 'json',
					header: {
						'content-type': 'application/json', // 设置content-type为application/json
						'satoken': satoken
					},
					success: (res) => {
						console.log('成功', res);
						console.log(res.data);
						if (res.data.code == 200) {
							this.sendSetSuccess();
						}
						if (res.data.code == 500) {
							this.sendSetFail();
						}
					},
					fail: function(res) {
						this.sendSetFail();
						console.log('错误', res);
			
					}
				});
			},
		}
	};
</script>

<style scoped>
	.add-bike {
		max-width: 400px;
		margin: 0 auto;
		padding: 20px;
		text-align: center;
	}

	.title {
		font-size: 24px;
		margin-bottom: 20px;
	}

	.form {
		display: flex;
		flex-direction: column;
	}

	.label {
		font-size: 16px;
		margin-bottom: 5px;
	}

	.input {
		padding: 8px;
		border: 1px solid #ccc;
		border-radius: 4px;
		margin-bottom: 10px;
	}

	.button {
		background-color: #007bff;
		color: #fff;
		padding: 10px 20px;
		border: none;
		border-radius: 4px;
		cursor: pointer;
		font-size: 16px;
	}

	.button:hover {
		background-color: #0056b3;
	}
</style>